#!/usr/bin/env python

import sys, pygame, pygame.midi
import os #for calling xte to 'sendkeys' My not be the best way, but works...on Linux...if you have xautomation installed

# set up pygame
pygame.init()
pygame.midi.init()

# list all midi devices
for x in range( 0, pygame.midi.get_count() ):
  print pygame.midi.get_device_info(x)

# open a specific midi device
i = pygame.midi.Input(3) #change this to your device

# run the event loop
while True:
  if i.poll():
    midi_events = i.read(10)
    if (midi_events[0][0][2] != 0):
      key = str(midi_events[0][0][1])
      print "Key is " + key
      if (key == "43"):
        os.system("xte 'str test this out'")
      elif (key == "44"):
        os.system("xte 'str Hello World'")
      elif (key == "45"):
        os.system("xte 'str Hello Bob'")
      elif (key == "46"):
        os.system("xte 'str Good-bye bob'")
      elif (key == "47"):
        os.system("xte 'str World part 2'")
      elif (key == "48"):
        os.system("xte 'str Last one'")

  # wait 10ms - this is arbitrary, but wait(0) still resulted
  # in 100% cpu utilization
  pygame.time.wait(10)